/**
AbstractCustomeEntity.java
jackysoft-basic-core
2012-8-6下午5:39:40
@author 屈甲康
@QQ285799123
mailto:qujiakang@126.com
 **/
package org.jackysoft.entity;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;

import org.apache.struts2.json.annotations.JSON;
import org.bson.types.ObjectId;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.hibernate.annotations.TypeDef;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

/**
 * <pre>
 * 适合用户自定义主键，例如复合ID
 * 例如 
 *    
 *    @BID
 *    private BusinessPK  id;
 *    
 *    public class BusinessPK{
 *        
 *        @ID
 *        String  username;
 *        @ID
 *        String  userCode; 
 *    }
 * 用户用复杂Id定义业务Id，自己用@Id
 * 
 * </pre>
 * */
@MappedSuperclass
@TypeDef(name = "jodatime", typeClass = org.joda.time.contrib.hibernate.PersistentDateTime.class)
public abstract class AbstractCustomeEntity implements Identifiable, Cloneable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -5502736988315730458L;

	protected ObjectId objectId;

	protected boolean leaf;

	protected DBObject root = new BasicDBObject();
	protected Integer sort = 0;

	@Override
	public void setObjectId(ObjectId objectId) {
		this.objectId = objectId;

	}

	@Override
	@Transient
	@JSON(serialize = false)
	public ObjectId getObjectId() {
		return this.objectId;
	}

	@Override
	public void setLeaf(boolean leaf) {
		this.leaf = leaf;

	}

	@Override
	@Transient
	public Boolean isLeaf() {
		return leaf;
	}

	@Override
	public void setRoot(DBObject root) {
		this.root = root;

	}

	@Override
	@Transient
	@JSON(serialize = false)
	public DBObject getRoot() {
		return this.root;
	}

	@Override
	public void setSort(Integer sort) {
		this.sort = sort;
	}

	@Override
	@Column(columnDefinition = "int default 0")
	@NotFound(action = NotFoundAction.IGNORE)
	@JSON(serialize = false)
	public Integer getSort() {
		return this.sort;
	}
	

	public Object clone() {
		try {
			ByteArrayOutputStream bout = new ByteArrayOutputStream();
			ObjectOutputStream out = new ObjectOutputStream(bout);
			out.writeObject(this);
			out.close();

			ByteArrayInputStream bin = new ByteArrayInputStream(
					bout.toByteArray());
			ObjectInputStream in = new ObjectInputStream(bin);
			Object ret = in.readObject();
			return ret;

		} catch (Exception e) {
			return null;
		}
	}
	
}
